package com.example.practice;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.POST;


/**
 * A simple {@link Fragment} subclass.
 */
public class CarwzFragment extends Fragment implements View.OnClickListener {

    private View view;
    private EditText carwzText1;
    private Button carwzButton;
    private TextView carwzText2;

    public CarwzFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_carwz, container, false);
        initView();
        return view;
    }

    private void initView() {
        carwzText1 = (EditText) view.findViewById(R.id.carwzText1);
        carwzButton = (Button) view.findViewById(R.id.carwzButton);
        carwzButton.setOnClickListener(this);
        carwzText2 = (TextView) view.findViewById(R.id.carwzText2);
    }
    private interface BlogSever{
        @POST("getWeiZhangLog")
        Call<ResponseBody> getBlog(@Body RequestBody str);
    }
    @Override
    public void onClick(View view) {
        Retrofit retrofit1 = new Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/")
                .build();
        Map<String,String> map= new HashMap<>();
        map.put("CarNo","鲁"+carwzText1.getText().toString());
        String s = new Gson().toJson(map);
        Log.v("TAG","WWWW"+s);
        BlogSever blogsever = retrofit1.create(BlogSever.class);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"),s);
        Call<ResponseBody> blog = blogsever.getBlog(requestBody);
        blog.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String s = response.body().string();
                    List<Car6Users> list = new ArrayList<>();
                    Car6Users car6Users = new Gson().fromJson(s, Car6Users.class);
                    list.add(car6Users);
                    if(list.get(0).getData().size()==0){
                        carwzText2.setText("没有查询到"+carwzText1.getText().toString()+"车的违章数据");
                    }else {
                    if(list.get(0).getData().get(0).getLog().size()==0){
                        carwzText2.setText("没有查询到"+carwzText1.getText().toString()+"车的违章数据");
                    } else {
                        Intent intent = new Intent(getContext(),Main2Activity.class);
                        intent.putExtra("name",s);
                        startActivity(intent);
                    }}
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                carwzText1.setText("没有查询到"+carwzText1.getText().toString()+"车的违章数据");
            }
        });
    }


}
